package com.fin.quant.rpc.common;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;


import java.text.SimpleDateFormat;

/**
 */

public class Result<T> {

    public static final Result SUCCESS = createSuccessResult();
    public static final Result ERROR = createErrorResult();

    public static <T> Result<T> createSuccessResult(T data) {
        Result<T> result = new Result<>();
        result.setCode(0);
        result.setData(data);
        result.setMessage("success");
        return result;
    }

    public static <T> Result<T> createSuccessResult() {
        return createSuccessResult((T) "success");
    }

    public static <T> Result<T> createErrorResult() {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setData(null);
        result.setMessage("ERROR");
        return result;
    }

    public static <T> Result<T> createErrorResult(T data) {
        Result<T> result = new Result<>();
        result.setCode(500);
        result.setData(data);
        result.setMessage("ERROR");
        return result;
    }

    public static <T> Result<T> createErrorResult(int returnCode, T data) {
        Result<T> result = new Result<>();
        result.setCode(returnCode);
        result.setData(data);
        result.setMessage("ERROR");
        return result;
    }



    public static <T> Result<T> createErrorResult(int returnCode,String returnMsg) {
        Result<T> result = new Result<>();
        result.setCode(returnCode);
        result.setData(null);
        result.setMessage(returnMsg);
        return result;
    }

    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    private T data;

    private int code;

    private String message;


    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
